<template>
	<view class="content">
		<swiper class="swiperBox" v-for="(item,index) in data.banner_img" :key="index">
			<swiper-item>
				<image :src="item.url" class="swiperItem"></image>
			</swiper-item>
		</swiper>
		<view class="infoBox">
			<text class="name">{{data.name}}</text>
			<view class="priceBox">
				<text class="sellPrice">￥{{data.price}}</text>
				<!-- <text class="linePrice">￥{{data.old_price}}</text> -->
				<view class="sellNum">
					月销：{{data.sale_num}}
				</view>
			</view>
			<view class="line">
			</view>
			<text class="label">商品详情</text>
			<mp-html :content="data.details" />
		</view>
		<view class="cartBox">
			<text class="totalPrice">￥{{cartTotal.dishes_price || 0}}</text>
			<view class="add" @click="add()">
				加入购物车
			</view>
			<view class="submit" @click="submit">
				立即购买
			</view>
			<view class="cart" @click="showCart()">
				<image :src="baseUrl+'/img/juanjiahui/cart.png'" class="cartImg"></image>
				<view class="cartNum">{{cartTotal.dishes_num|| 0}}</view>
			</view>
		</view>
		<view class="cartContainer" v-if="isCart">
			<view class="cartInner">
				<image :src="baseUrl+'/img/juanjiahui/close.png'" class="close" @click.stop="showCart"></image>
				<view class="cartLabel">
					购物车
				</view>
				<view class="selectedBox">
					<text class="selectedLabe">已选择商品（{{cartTotal.dishes_num || 0 }}件）</text>
					<view class="deletedRight" @click="clear()">
						<image :src="baseUrl+'/img/juanjiahui/delete.png'" class="deletedIcon"></image>
						<text class="deleteText">清空购物车</text>
					</view>
				</view>
				<view class="cartItemBox">
					<view class="cartItem" v-for="(item,index) in cartTotal.dishes" :key="index">
						<image :src="item.cover" class="cartPoster"></image>
						<text class="cartName">{{item.name}}</text>
						<view class="cartPriceBox">
							<text class="sellPrice">￥{{item.price}}</text>
							<text class="linePrice">￥{{item.old_price}}</text>
						</view>
						<view class="countNumBox">
							<view class="reduce" @click.stop="del(item.dishes_id)">
								-
							</view>
							<input type="text" disabled="true" class="countNum" :value="item.num" />
							<view class="add" @click.stop="add(item.dishes_id,1,item.cat_id)">
								+
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				isCart: false,
				baseUrl: this.$baseUrl,
				id: '',
				data: '',
				cartTotal: '', //购物车商品
			}

		},
		onLoad(options) {
			console.log(options.id);
			this.id = options.id;
			this.$request.get(`dishes_read/${this.id}`).then(res => {
				this.data = res.data.data;
			})
			this.getCart()
		},
		methods: {
			clear() { //清空购物车
				if (this.cartTotal.dishes.length <= 0) {
					return this.$toast('购物车暂无商品');
				}
				uni.showModal({
					title: '提示',
					content: '是否确认清空购物车',
					success: (res) => {
						if (res.confirm) {
							this.$request.get(`user_del_cart`).then(res => {
								if (res.data.code == 10000) { // 更新购物车
									this.getCart();
									this.$toast('清除成功');
								} else {
									this.$toast('清除失败');
								}
							});
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			del(id) {
				console.log("删除商品" + id);
				this.$request.get(`del_cart?id=${id}`).then(res => {
					if (res.data.code == 10000) { // 更新购物车
						this.getCart();
					} else {
						this.$toast('删除失败');
					}
				});

			},
			showCart() {
				this.isCart = !this.isCart;
			},
			submit() {
				if (this.cartTotal.dishes <= 0) {
					return this.$toast('请先添加商品');
				}
				this.$store.commit('setTempData', this.$data);
				this.$navto.navto("/pages/juanjiahui_dinner_submit/juanjiahui_dinner_submit")
			},
			add(dishes_id, num, cat_id) {
				console.log("加入购物车");
				this.$request.post(`add_cart`, {
					dishes_id: this.id,
					num: 1,
					cat_id: this.data.cat_id
				}).then(res => {
					if (res.data.code == 10000) { // 更新购物车
						this.getCart();
					} else {
						this.$toast(res.data.message);
					}
				});
			},
			getCart() {
				this.$request.get(`user_cart_list`).then(res => {
					this.cartTotal = res.data.data;
					//分类商品
				}).catch(error => {
					console.error("获取购物车数据失败:", error);
				});
			}
		}
	}
</script>
<style lang="scss" scoped>
	.cartContainer {
		position: fixed;
		bottom: 0;
		z-index: 2;
		width: 100%;
		height: 100vh;
		background-color: rgba(0, 0, 0, 0.6);

		.cartInner {
			width: 100%;
			position: fixed;
			bottom: 0%;
			z-index: 2;
			background-color: #ffffff;
			padding: 40rpx 40rpx 150rpx 40rpx;
			box-sizing: border-box;
			display: flex;
			// align-items: center;
			flex-direction: column;

			.close {
				position: absolute;
				right: 6%;
				top: 4vw;
				width: 60rpx;
				height: 60rpx;
			}

			.cartItemBox {
				position: relative;
				width: 670rpx;
				max-height: 60vh;
				overflow-y: scroll;
				// background-color: red;

				.cartItem {
					position: relative;
					width: 670rpx;
					height: 140rpx;
					margin-bottom: 45rpx;

					.countNumBox {
						display: flex;
						align-items: center;
						position: absolute;
						top: 50%;
						transform: translateY(-50%);
						right: 30rpx;

						.add {
							display: flex;
							justify-content: center;
							height: 40rpx;
							line-height: 40rpx;
							font-size: 40rpx;
							width: 40rpx;
							border-radius: 50%;
							border: 1rpx solid #121212;
						}

						.reduce {
							display: flex;
							justify-content: center;
							height: 40rpx;
							line-height: 40rpx;
							font-size: 40rpx;
							width: 40rpx;
							border-radius: 50%;
							border: 1rpx solid #121212;
						}

						.countNum {
							width: 60rpx;
							text-align: center;
							font-size: 30rpx;
						}
					}

					.cartPriceBox {
						position: absolute;
						top: 85rpx;
						left: 240rpx;
						display: flex;
						align-items: center;

						.linePrice {
							color: #8e8e8e;
							font-weight: 500;
							font-size: 18rpx;
							margin-left: 20rpx;
							text-decoration: line-through;
						}

						.sellPrice {
							color: #121212;
							font-weight: 700;
							font-size: 22rpx;
						}
					}

					.cartName {
						position: absolute;
						font-weight: 700;
						font-size: 26rpx;
						color: #121212;
						top: 20rpx;
						left: 240rpx;
					}

					.cartPoster {
						width: 200rpx;
						height: 140rpx;
					}
				}
			}

			.selectedBox {
				display: flex;
				justify-content: space-between;
				margin-bottom: 60rpx;

				.deletedRight {
					display: flex;
					align-items: center;

					.deleteText {
						font-weight: 500;
						font-size: 20rpx;
						color: #121212;
						margin-left: 15rpx;
					}

					.deletedIcon {
						width: 26rpx;
						height: 24rpx;
					}
				}

				.selectedLabe {
					font-weight: 500;
					font-size: 24rpx;
					color: #121212;
				}
			}

			.cartLabel {
				font-weight: 700;
				font-size: 30rpx;
				color: #121212;
				padding-bottom: 35rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				border-bottom: 1rpx solid #000000;
				width: 100%;
				margin-bottom: 50rpx;
			}
		}
	}

	.infoBox {
		display: flex;
		flex-direction: column;
		padding: 50rpx 40rpx 50rpx 40rpx;
		border-radius: 20rpx 20rpx 0 0;
		background-color: #ffffff;
		margin-top: -20rpx;
		position: relative;

		.label {
			font-weight: 700;
			font-size: 24rpx;
			color: #121212;
			margin-bottom: 50rpx;
		}

		.line {
			width: 100%;
			height: 1rpx;
			background-color: #121212;
			margin-top: 80rpx;
			margin-bottom: 80rpx;
		}

		.priceBox {
			margin-top: 45rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.sellNum {
				font-weight: 500;
				font-size: 22rpx;
				color: #8E8E8E;
				margin-left: auto;
			}

			.linePrice {
				color: #8e8e8e;
				font-weight: 500;
				font-size: 22rpx;
				margin-left: 20rpx;
				text-decoration: line-through;
			}

			.sellPrice {
				color: #121212;
				font-weight: 700;
				font-size: 30rpx;
			}
		}

		.canshiIName {
			position: absolute;
			font-weight: 700;
			font-size: 28rpx;
			color: #121212;
			top: 60rpx;
			left: 340rpx;
		}
	}

	.cartBox {
		position: fixed;
		bottom: 0;
		width: 100%;
		height: 130rpx;
		background-color: #ffffff;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		padding-left: 40rpx;
		padding-right: 40rpx;
		justify-content: space-between;
		padding-bottom: 20rpx;
		border-top: 1rpx solid #f0f0f0;

		.cart {
			position: absolute;
			left: 45rpx;
			top: -45rpx;

			.cartNum {
				position: absolute;
				top: -5rpx;
				right: -10rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				width: 35rpx;
				line-height: 35rpx;
				background-color: #FF7B6C;
				border-radius: 50%;
				font-size: 16rpx;
				color: #fff;
				box-sizing: border-box;
			}

			.cartImg {
				width: 80rpx;
				height: 80rpx;
			}
		}

		.totalPrice {
			color: #121212;
			font-weight: 700;
			font-size: 34rpx;
			letter-spacing: 2rpx;
		}

		.add {
			width: 245rpx;
			height: 80rpx;
			border-radius: 40rpx;
			border: 1rpx solid #121212;
			display: flex;
			align-items: center;
			justify-content: center;
			font-weight: 700;
			font-size: 28rpx;
			color: #121212;
			letter-spacing: 5rpx;
			box-sizing: border-box;
		}

		.submit {
			width: 245rpx;
			height: 80rpx;
			border-radius: 40rpx;
			background: #121212;
			display: flex;
			align-items: center;
			justify-content: center;
			font-weight: 700;
			font-size: 28rpx;
			color: #FFFFFF;
			letter-spacing: 5rpx;
		}
	}

	.swiperBox {
		width: 100%;
		height: 560rpx;

		.swiperItem {
			width: 100%;
			height: 100%;
		}
	}

	.content {
		width: 100%;
		height: 100%;
		padding-bottom: 200rpx;
	}
</style>